package cn.tansanqinger.service.knowledge.impl;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.tansanqinger.dao.sql.knowledge.KnowledgeSqlDaoImpl;
import cn.tansanqinger.pojo.Collecttopic;
import cn.tansanqinger.pojo.Knowledge;
import cn.tansanqinger.pojo.Queryknowledge;
import cn.tansanqinger.pojo.User;
import cn.tansanqinger.service.knowledge.IKnowledgeService;
import cn.tansanqinger.util.DateUtil;
import cn.tansanqinger.util.PageData;

@Service
public class KnowledgeServiceImpl implements IKnowledgeService{
	
	@Autowired
	private KnowledgeSqlDaoImpl impl;

	@Override
	public Knowledge selectOne(HttpServletRequest request) {
		PageData pd = new PageData(request);
		return impl.selectOne(Integer.parseInt(pd.get("kid")+""));
	}

	@Override
	public PageData collectKnowledge(HttpServletRequest request) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PageData shieldKnowledge(HttpServletRequest request) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PageData reportKnowledge(HttpServletRequest request) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PageData queryKnowledge(HttpServletRequest request) {
		PageData pd = new PageData(request);
		String id = pd.get("id")+"";
		if(!id.matches("\\d+")) {
			pd.put("code", "-1");
			pd.put("msg", "数据异常");
			return pd;
		}
		User user = (User) request.getSession().getAttribute("user");
		Queryknowledge queryknowledge = impl.selectQidOne(user.getId(), Integer.parseInt(id));
		if(queryknowledge!=null) {
			queryknowledge.setQmessage(queryknowledge.getQmessage()+"  |再次提交疑问|  "+pd.get("message")+"");
			if(1==impl.modificationQuery(queryknowledge)) {
				pd.put("code", "0");
				pd.put("msg", "提交成功");
			} else {
				pd.put("code", "-1");
				pd.put("msg", "提交失败");
			}
		} else {
			queryknowledge = new Queryknowledge();
			queryknowledge.setUid(user.getId());
			queryknowledge.setQmessage(pd.get("message")+"");
			queryknowledge.setKid(Integer.parseInt(id));
			queryknowledge.setQtime(DateUtil.getTime());
			queryknowledge.setQisdispose(0);
			if(1==impl.addQuery(queryknowledge)) {
				Knowledge knowledge = impl.selectOne(Integer.parseInt(id));
				knowledge.setKisquery(1);
				impl.modificationKnowledge(knowledge);
				pd.put("code", "0");
				pd.put("msg", "提交成功");
			} else {
				pd.put("code", "-1");
				pd.put("msg", "提交失败");
			}
		}
		return pd;
	}

	@Override
	public Collecttopic isCollectKnowledge(int o1, int o2) {
		// TODO Auto-generated method stub
		return null;
	}


}
